package com.jerei.plugin.dao.impl;

import com.jerei.plugin.dao.IWcmCommonResourceDao;
import com.jerei.plugin.entity.WcmCommonResource;
import com.jerei.plugin.utils.JrJdbcTemplate;
import com.jerei.utils.condition.ConditionEntity;
import com.jerei.utils.condition.ConditionTools;
import com.jerei.utils.condition.SQLEntity;
import com.jerei.utils.spring.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by sunfeihu on 2016/10/20.
 */
@Repository
public class WcmCommonResourceDaoImpl implements IWcmCommonResourceDao {

    @Autowired
    private JrJdbcTemplate jrJdbcTemplate;

    @Override
    public WcmCommonResource save(WcmCommonResource entity) {
        final String sql = " insert into wcm_common_resource (uuid,add_date,add_ip,add_user,update_date,update_ip,update_user,sid,title,file_path,old_file_name,file_size,file_type,file_extension,down_count,width,height,upload_user_id,upload_member_id,folder_id,tags,file_m_path,file_s_path,flile_l_path) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        KeyHolder keyHolder = new GeneratedKeyHolder();
        final WcmCommonResource finalEntity = entity;
        this.jrJdbcTemplate.getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
                ps.setObject(1, finalEntity.getUuid());
                ps.setObject(2, new Timestamp(finalEntity.getAddDate().getTime()));
                ps.setObject(3, finalEntity.getAddIp());
                ps.setObject(4, finalEntity.getAddUser());
                ps.setObject(5, new Timestamp(finalEntity.getUpdateDate().getTime()));
                ps.setObject(6, finalEntity.getUpdateIp());
                ps.setObject(7, finalEntity.getUpdateUser());
                ps.setObject(8, finalEntity.getSid());
                ps.setObject(9, finalEntity.getTitle());
                ps.setObject(10, finalEntity.getFilePath());
                ps.setObject(11, finalEntity.getOldFileName());
                ps.setObject(12, finalEntity.getFileSize());
                ps.setObject(13, finalEntity.getFileType());
                ps.setObject(14, finalEntity.getFileExtension());
                ps.setObject(15, finalEntity.getDownCount());
                ps.setObject(16, finalEntity.getWidth());
                ps.setObject(17, finalEntity.getHeight());
                ps.setObject(18, finalEntity.getUploadUserId());
                ps.setObject(19, finalEntity.getUploadMemberId());
                ps.setObject(20, finalEntity.getFolderId());
                ps.setObject(21, finalEntity.getTags());
                ps.setObject(22, finalEntity.getFileMPath());
                ps.setObject(23, finalEntity.getFileSPath());
                ps.setObject(24, finalEntity.getFlileLPath());
                return ps;
            }
        }, keyHolder);
        Long id = keyHolder.getKey().longValue();
        return this.find(id);
    }

    @Override
    public int delete(Long id) {
        final String sql = " delete from wcm_common_resource where id = ? ";
        return this.jrJdbcTemplate.getJdbcTemplate().update(sql, new Object[]{
                id
        });
    }

    @Override
    public int update(WcmCommonResource entity) {
        final String sql = " update wcm_common_resource set uuid=?,add_date=?,add_ip=?,add_user=?,update_date=?,update_ip=?,update_user=?,sid=?,title=?,file_path=?,old_file_name=?,file_size=?,file_type=?,file_extension=?,down_count=?,width=?,height=?,upload_user_id=?,upload_member_id=?,folder_id=?,tags=?,file_m_path=?,file_s_path=?,flile_l_path=? where id = ?";
        return this.jrJdbcTemplate.getJdbcTemplate().update(sql, new Object[]{
                entity.getUuid(),
                entity.getAddDate(),
                entity.getAddIp(),
                entity.getAddUser(),
                entity.getUpdateDate(),
                entity.getUpdateIp(),
                entity.getUpdateUser(),
                entity.getSid(),
                entity.getTitle(),
                entity.getFilePath(),
                entity.getOldFileName(),
                entity.getFileSize(),
                entity.getFileType(),
                entity.getFileExtension(),
                entity.getDownCount(),
                entity.getWidth(),
                entity.getHeight(),
                entity.getUploadUserId(),
                entity.getUploadMemberId(),
                entity.getFolderId(),
                entity.getTags(),
                entity.getFileMPath(),
                entity.getFileSPath(),
                entity.getFlileLPath(),
                entity.getId()
        });
    }

    @Override
    public WcmCommonResource find(Long id) {
        final String sql = " select * from wcm_common_resource where id = ? ";
        return this.jrJdbcTemplate.getJdbcTemplate().queryForObject(sql, new Object[]{id}, new WcmCommonResourceRowerMapper());
    }

    @Override
    public void save(List<WcmCommonResource> list) {
        if (null != list && list.size() > 0) {
            final String sql = " insert into wcm_common_resource (uuid,add_date,add_ip,add_user,update_date,update_ip,update_user,sid,title,file_path,old_file_name,file_size,file_type,file_extension,down_count,width,height,upload_user_id,upload_member_id,folder_id,tags,file_m_path,file_s_path,flile_l_path) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            List<Object[]> params = new ArrayList<Object[]>();
            Object[] objArr = null;
            WcmCommonResource entity = null;
            for (int i = 0; i < list.size(); i++) {
                entity = list.get(i);
                objArr = new Object[24];
                objArr[0] = entity.getUuid();
                objArr[1] = entity.getAddDate();
                objArr[2] = entity.getAddIp();
                objArr[3] = entity.getAddUser();
                objArr[4] = entity.getUpdateDate();
                objArr[5] = entity.getUpdateIp();
                objArr[6] = entity.getUpdateUser();
                objArr[7] = entity.getSid();
                objArr[8] = entity.getTitle();
                objArr[9] = entity.getFilePath();
                objArr[10] = entity.getOldFileName();
                objArr[11] = entity.getFileSize();
                objArr[12] = entity.getFileType();
                objArr[13] = entity.getFileExtension();
                objArr[14] = entity.getDownCount();
                objArr[15] = entity.getWidth();
                objArr[16] = entity.getHeight();
                objArr[17] = entity.getUploadUserId();
                objArr[18] = entity.getUploadMemberId();
                objArr[19] = entity.getFolderId();
                objArr[20] = entity.getTags();
                objArr[21] = entity.getFileMPath();
                objArr[22] = entity.getFileSPath();
                objArr[23] = entity.getFlileLPath();
                params.add(objArr);
            }
            this.jrJdbcTemplate.getJdbcTemplate().batchUpdate(sql, params);
        }
    }

    @Override
    public void delete(List<Long> ids) {
        if (null != ids && ids.size() > 0) {
            final String sql = " delete from wcm_common_resource where id = ? ";
            List<Object[]> params = new ArrayList<Object[]>();
            Object[] objArr = null;
            for (int i = 0; i < ids.size(); i++) {
                objArr = new Object[1];
                objArr[0] = ids.get(i);
                params.add(objArr);
            }
            this.jrJdbcTemplate.getJdbcTemplate().batchUpdate(sql, params);
        }
    }

    @Override
    public void update(List<WcmCommonResource> list) {
        if (null != list && list.size() > 0) {
            final String sql = " update wcm_common_resource set uuid=?,add_date=?,add_ip=?,add_user=?,update_date=?,update_ip=?,update_user=?,sid=?,title=?,file_path=?,old_file_name=?,file_size=?,file_type=?,file_extension=?,down_count=?,width=?,height=?,upload_user_id=?,upload_member_id=?,folder_id=?,tags=?,file_m_path=?,file_s_path=?,flile_l_path=? where id = ?";
            List<Object[]> params = new ArrayList<Object[]>();
            Object[] objArr = null;
            WcmCommonResource entity = null;
            for (int i = 0; i < list.size(); i++) {
                entity = list.get(i);
                objArr = new Object[24];
                objArr[0] = entity.getUuid();
                objArr[1] = entity.getAddDate();
                objArr[2] = entity.getAddIp();
                objArr[3] = entity.getAddUser();
                objArr[4] = entity.getUpdateDate();
                objArr[5] = entity.getUpdateIp();
                objArr[6] = entity.getUpdateUser();
                objArr[7] = entity.getSid();
                objArr[8] = entity.getTitle();
                objArr[9] = entity.getFilePath();
                objArr[10] = entity.getOldFileName();
                objArr[11] = entity.getFileSize();
                objArr[12] = entity.getFileType();
                objArr[13] = entity.getFileExtension();
                objArr[14] = entity.getDownCount();
                objArr[15] = entity.getWidth();
                objArr[16] = entity.getHeight();
                objArr[17] = entity.getUploadUserId();
                objArr[18] = entity.getUploadMemberId();
                objArr[19] = entity.getFolderId();
                objArr[20] = entity.getTags();
                objArr[21] = entity.getFileMPath();
                objArr[22] = entity.getFileSPath();
                objArr[23] = entity.getFlileLPath();
                objArr[24] = entity.getId();
                params.add(objArr);
            }
            this.jrJdbcTemplate.getJdbcTemplate().batchUpdate(sql, params);
        }
    }

    @Override
    public PageBean<WcmCommonResource> page(int nowPage, int pageSize, Long sid) {
        final String sql = " select * from wcm_common_resource where sid = ? order by id desc ";
        return new PageBean<WcmCommonResource>(sql, new Object[]{sid}, nowPage, pageSize, new WcmCommonResourceRowerMapper(), this.jrJdbcTemplate.getJdbcTemplate());
    }

    @Override
    public PageBean<WcmCommonResource> page(int nowPage, int pageSize, ConditionEntity conditionEntity) {
        SQLEntity sqlEntity = ConditionTools.conditionEntities2SQL(conditionEntity);
        final String sql = " select * from wcm_common_resource where 1 = 1 " + sqlEntity.getSql() + " order by " + sqlEntity.getOrder();
        return new PageBean<WcmCommonResource>(sql, sqlEntity.getParams(), nowPage, pageSize, new WcmCommonResourceRowerMapper(), this.jrJdbcTemplate.getJdbcTemplate());
    }

    @Override
    public PageBean<WcmCommonResource> pageByNull(int nowPage, int pageSize) {
        final String sql = " select * from wcm_common_resource where 1 = 1 and folderId is null order by id desc ";
        return new PageBean<WcmCommonResource>(sql, new Object[]{null}, nowPage, pageSize, new WcmCommonResourceRowerMapper(), this.jrJdbcTemplate.getJdbcTemplate());
    }
}


class WcmCommonResourceRowerMapper implements RowMapper<WcmCommonResource> {
    @Override
    public WcmCommonResource mapRow(ResultSet rs, int rowNum) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        Set<String> fieldSet = new HashSet<String>();
        for (int i = 1; i <= columnCount; i++) {
            fieldSet.add(rsmd.getColumnName(i));
        }
        WcmCommonResource entity = new WcmCommonResource();
        if (fieldSet.contains("id")) {
            entity.setId(rs.getLong("id"));
        }
        if (fieldSet.contains("uuid")) {
            entity.setUuid(rs.getString("uuid"));
        }
        if (fieldSet.contains("add_date")) {
            entity.setAddDate(new java.util.Date(rs.getTimestamp("add_date").getTime()));
        }
        if (fieldSet.contains("add_ip")) {
            entity.setAddIp(rs.getString("add_ip"));
        }
        if (fieldSet.contains("add_user")) {
            entity.setAddUser(rs.getString("add_user"));
        }
        if (fieldSet.contains("update_date")) {
            entity.setUpdateDate(new java.util.Date(rs.getTimestamp("update_date").getTime()));
        }
        if (fieldSet.contains("update_ip")) {
            entity.setUpdateIp(rs.getString("update_ip"));
        }
        if (fieldSet.contains("update_user")) {
            entity.setUpdateUser(rs.getString("update_user"));
        }
        if (fieldSet.contains("sid")) {
            entity.setSid(rs.getLong("sid"));
        }
        if (fieldSet.contains("title")) {
            entity.setTitle(rs.getString("title"));
        }
        if (fieldSet.contains("file_path")) {
            entity.setFilePath(rs.getString("file_path"));
        }
        if (fieldSet.contains("old_file_name")) {
            entity.setOldFileName(rs.getString("old_file_name"));
        }
        if (fieldSet.contains("file_size")) {
            entity.setFileSize(rs.getString("file_size"));
        }
        if (fieldSet.contains("file_type")) {
            entity.setFileType(rs.getInt("file_type"));
        }
        if (fieldSet.contains("file_extension")) {
            entity.setFileExtension(rs.getString("file_extension"));
        }
        if (fieldSet.contains("down_count")) {
            entity.setDownCount(rs.getInt("down_count"));
        }
        if (fieldSet.contains("width")) {
            entity.setWidth(rs.getInt("width"));
        }
        if (fieldSet.contains("height")) {
            entity.setHeight(rs.getInt("height"));
        }
        if (fieldSet.contains("upload_user_id")) {
            entity.setUploadUserId(rs.getLong("upload_user_id"));
        }
        if (fieldSet.contains("upload_member_id")) {
            entity.setUploadMemberId(rs.getLong("upload_member_id"));
        }
        if (fieldSet.contains("folder_id")) {
            entity.setFolderId(rs.getLong("folder_id"));
        }
        if (fieldSet.contains("tags")) {
            entity.setTags(rs.getString("tags"));
        }
        if (fieldSet.contains("file_m_path")) {
            entity.setFileMPath(rs.getString("file_m_path"));
        }
        if (fieldSet.contains("file_s_path")) {
            entity.setFileSPath(rs.getString("file_s_path"));
        }
        if (fieldSet.contains("flile_l_path")) {
            entity.setFlileLPath(rs.getString("flile_l_path"));
        }
        return entity;
    }
}